﻿#include "Flower.h"
#include "ResourceManager.h"
#include <d3dx9.h>

Flower::Flower(int id,float x, float y, int type, int state)
{
	_sprite = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Item),300, 0);
	_id = id;
	_x = x;
	_y = y;
	_type = type;
	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();
	_state = state;
	_xPre = x;
	_yPre = y - 32;
	_vx = 0;
	_vy = 0;
	_accelX = 0;
	_accelY = 0;
	_isMushroom = true;
	_isGravity = true;

	if (type == 16)		//nam mang
	{
		if (_state == IS_IDLE)
		{
			_sprite->SetFrame(7,7);	//frame an
		} 
		else if (_state == IS_MOVING)
		{
			_isMushroom = true;
			_sprite->SetFrame(4,4);
			if (_direction == 1) //sang trai
			{
				_vx = -0.2;
			} 
			else if (_direction == 2) //sang trai
			{ 
				_vx = 0.2;
			}

		}
	} 
	else if (type == 15 || type == 17)	//nam to, hoa
	{
		if (_state == IS_IDLE)
		{
			_sprite->SetFrame(7,7);	//frame an
		} 
		else if (_state == IS_MOVING)
		{
			_isMushroom = true;
			_sprite->SetFrame(2,2);
			if (_direction == 1) //sang trai
			{
				_vx = -0.2;
			} 
			else if (_direction == 2) //sang trai
			{ 
				_vx = 0.2;
			}

		}
		else if (_state == IS_READY)
		{
			_isMushroom = false;
			_sprite->SetFrame(5,5);
		}
	}
}

Flower::~Flower(void)
{
	if (_sprite)
	{
		delete _sprite;
	}
	
}

void Flower::Update(list<Object *> *_list, float time)
{
	switch(_state)
	{
	case IS_IDLE:
		break;
	case IS_ACTIVING:

		_vy -= _accelY * time;
		_y += _vy * time + 1.0f/2.0f * _accelY * time * time;
		if(_y > _yPre + 32)
			if(_isMushroom == true)
			{
				if (this->getIDObjType() == 16)		//nam mang
				{
					_sprite->SetFrame(4,4);
				}
				else								//nam lon
				{
					_sprite->SetFrame(2,2);
				}

				changeState(IS_MOVING);
			}
			else
			{
				_sprite->SetFrame(5,5);
				changeState(IS_READY);
			}
			break;
	case IS_MOVING:
		Move(_list, time);
		break;
	case IS_READY:

		break;
	}


	_sprite->Update(time);
}


void Flower:: Render(int vpX, int vpY)
{
	if (_state != IS_REMOVE)
	{
		_sprite->Render(_x, _y, vpX, vpY);
	}
	
}
void Flower::Move(list<Object*>* _list, float time)
{

	Object* obj;
	list<Object*>::iterator i;

	//Theo phương x
	_x += _vx * time;

	//Theo phương y
	if (_isGravity == false)
	{
		_accelY = 0;
	}
	else
	{
		_accelY = FLOWER_GRAVITY;
	}
	
	_vy -= _accelY * time;
	_y += _vy * time + 1.0/2.0f * _accelY * time * time;


	for (i = _list->begin(); i != _list->end(); i++)
	{
		obj = *i;
		if (obj->getObjectType() == OBJECT_LAND)
		{
			Box box(this->_xPre,
				this->_yPre - this->_height, 
				_width, 
				_height, 
				_vx * time, 
				_vy * time + 1.0f/2.0f * _accelY * time * time);

			Box block(obj->getPosX(),
				obj->getPosY() - obj->getHeight(),// - 0.11,
				obj->getWidth(),
				obj->getHeight()
				);

			Box boxphase = Collision::_GetSweptBroadphaseBox(box);
			if (Collision::_AABBCheck(boxphase, block))
			{
				float normalx = 0;
				float normaly = 0;
				float collision_time = Collision::_SweptAABB(box, block, normalx, normaly);
				if (0 < collision_time && collision_time < 1)
				{
					
					if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
					{
						_x = _xPre + _vx * collision_time;
						_accelX = 0;
						_vx = -_vx;

					} 
					else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
					{
						_x = _xPre + _vx * collision_time;
						_accelX = 0;
						_vx = -_vx;
					}
					else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
					{
					}
					else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
					{
						_y = _yPre + _vy * collision_time + 1.0f/2.0f * _accelY * collision_time *collision_time + 0.5;
						_accelY = 0;
						_vy = 0;

					}

					if (this->_yPre - this->_height <= obj->getPosY() + 1.5)
					{
						_isGravity = false;
						_y = this->_height + obj->getPosY() + 1.5;
					}
					else
					{
						_isGravity = true;
					}
					 
				}
				else
				{
				}
			}
			else
			{
				if (this->_yPre - this->_height > obj->getPosY() + 1.5)
				{
					_isGravity = true;

				}
			}

			 
		}
	}
	 
	_xPre = _x;
	_yPre = _y;

	if (_vx >= 0)
	{
		
		_direction = 2;
	} 
	else
	{
		
		_direction = 1;
	}
}

void Flower::changeState(int state)
{
	_state = state;
	switch(_state)
	{
	case IS_ACTIVING:
		_vy = 6.0;
		_accelY = FLOWER_GRAVITY;
		break;

	case IS_MOVING:
		_vy = 0;
		if(_vx == 0.100)
			_vx = -0.200;
		else 
			_vx = 0.200;
		_accelY = FLOWER_GRAVITY;
		break;

	case IS_READY:
		_y = _yPre + 32;
		break;

	case IS_FLOWER:
		_isMushroom = false;
		changeState(IS_ACTIVING);
		break;

	case IS_REMOVE:
		break;
	}
}

OBJECT_TYPE Flower::getObjectType()
{
	return OBJECT_ITEM;
}